package com.boss.salary.controller;

import com.boss.api.dto.ApiResultDTO;
import com.boss.common.base.BasicinfoBaseController;
import com.boss.common.entity.PageDTO;
import com.boss.common.util.PageUtils;
import com.boss.framework.dto.PaginationDTO;
import com.boss.salary.entity.FormulaManage;
import com.boss.salary.entity.SalaryBasedata;
import com.boss.salary.service.IFormulaManageService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/formulaManageController")
public class FormulaManageController extends BasicinfoBaseController {

    /**
     * 引入日志框架常量
     */
    private final static Logger logger = LoggerFactory.getLogger(FormulaManageController.class);

    @Autowired
    private IFormulaManageService formulaManageService;

    @GetMapping("/queryFormulaManagePage")
    public ApiResultDTO queryFormulaManagePage(@RequestHeader("X-Token") String tokenid,
                                               @RequestParam("page") String page,
                                               @RequestParam("limit") String limit,
                                               @RequestParam("agencyId") String agencyId,
                                               @RequestParam Map<String, Object> queryForm) {
        try {
            PaginationDTO paginationDTO = PageUtils.buildPaginationDTOPage(page, limit);
            queryForm.put("mof_div_code",getUser(tokenid).getProvince());
            PageDTO pageDTO = formulaManageService.getFormulaManagePage(paginationDTO, agencyId, queryForm);
            return ApiResultDTO.success(pageDTO);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return ApiResultDTO.error(ERROR_RSCODE, "查询失败！");
        }
    }


    /**
     * 查询新增表单中的工资列表
     *
     * @return
     */
    @GetMapping("/queryAllSalary")
    public ApiResultDTO queryAllSalary(@RequestHeader("X-Token") String tokenid) {
        try {
            String province = getUser(tokenid).getProvince();
            List<SalaryBasedata> salaryList = formulaManageService.getAllSalary(province);
            return ApiResultDTO.success(salaryList);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return ApiResultDTO.error(ERROR_RSCODE, "查询失败！");
        }
    }


    @PostMapping("/submitFormula")
    public ApiResultDTO submitFormula(@RequestHeader("X-Token") String tokenid,
                                      @RequestBody FormulaManage formulaManage) {
        try {
            String province = getUser(tokenid).getProvince();
            formulaManage.setMofDivCode(province);
            formulaManageService.submitFormula(formulaManage);
            return ApiResultDTO.success();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return ApiResultDTO.error(ERROR_RSCODE, "保存失败！");
        }
    }

    @GetMapping("/queryFormulaDetail")
    public ApiResultDTO queryFormulaDetail(@RequestParam("id") String id) {
        try {
            FormulaManage formulaManage = formulaManageService.getFormulaDetail(id);
            return ApiResultDTO.success(formulaManage);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return ApiResultDTO.error(ERROR_RSCODE, "查询失败！");
        }
    }

    @PostMapping("/removeFormulaById/{id}")
    public ApiResultDTO removeFormulaById(@PathVariable("id") String id) {
        try {
            formulaManageService.removeFormulaById(id);
            return ApiResultDTO.success();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return ApiResultDTO.error(ERROR_RSCODE, "操作失败！");
        }
    }

    // 复制公式
    @PostMapping("/saveCopyFormula")
    public ApiResultDTO saveCopyFormula(@RequestHeader("X-Token") String tokenid,
                                        @RequestBody FormulaManage formulaManage) {
        try {
            String province = getUser(tokenid).getProvince();
            formulaManageService.saveCopyFormula(formulaManage,province);
            return ApiResultDTO.success();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return ApiResultDTO.error(ERROR_RSCODE, "保存失败！");
        }
    }

}
